﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Configuration;
using Abp.Zero.Configuration;
using Microsoft.Extensions.Configuration;
using MyCompanyName.AbpZeroTemplate.Configuration;

namespace AVISOutBound.SSOServer.Configuration
{
    /// <summary>
    /// Defines settings for the application.
    /// See <see cref="AppSettings"/> for setting names.
    /// </summary>
    public class AppSettingProvider : SettingProvider
    {
        private readonly IConfigurationRoot _appConfiguration;

        public AppSettingProvider(IAppConfigurationAccessor configurationAccessor)
        {
            _appConfiguration = configurationAccessor.Configuration;
        }

        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {

            return GetAlipaySettings();
        }

        private IEnumerable<SettingDefinition> GetAlipaySettings()
        {
            return new[] {
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Url, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Url, ""), isVisibleToClients: false),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Appid, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Appid, "")),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.PrivateKey, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.PrivateKey, ""), isVisibleToClients: false),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.PublicKey, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.PublicKey, "")),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Ppid, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Ppid, ""), isVisibleToClients: false),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Format, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Format, ""), isVisibleToClients: false),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Charset, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Charset, ""), isVisibleToClients: false),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Sign_type, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Sign_type, "")),
                new SettingDefinition(AppSettings.PaymentConfig.Alipay.Version, GetFromAppSettings(AppSettings.PaymentConfig.Alipay.Version, "")),

                new SettingDefinition(AppSettings.PaymentConfig.Wechat.AppId,GetFromAppSettings(AppSettings.PaymentConfig.Wechat.AppId,"")),
                new SettingDefinition(AppSettings.PaymentConfig.Wechat.MchId,GetFromAppSettings(AppSettings.PaymentConfig.Wechat.MchId,"")),
                new SettingDefinition(AppSettings.PaymentConfig.Wechat.PayKey,GetFromAppSettings(AppSettings.PaymentConfig.Wechat.PayKey,"")),
                new SettingDefinition(AppSettings.PaymentConfig.Wechat.Password,GetFromAppSettings(AppSettings.PaymentConfig.Wechat.Password,"")),
                new SettingDefinition(AppSettings.PaymentConfig.Wechat.CertPath,GetFromAppSettings(AppSettings.PaymentConfig.Wechat.CertPath,"")),
            };
        }


        private string GetFromAppSettings(string name, string defaultValue = null)
        {
            return GetFromSettings("App:" + name, defaultValue);
        }

        private string GetFromSettings(string name, string defaultValue = null)
        {
            return _appConfiguration[name] ?? defaultValue;
        }
    }
}
